package lyx.tank.element
{
	import flash.display.BitmapData;
	import flash.display.Graphics;
	import flash.display.Sprite;
	import flash.geom.Matrix;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.utils.getTimer;
	
	import lyx.game.element.DynamicGameElement;
	import lyx.game.element.GameElement;
	import lyx.game.utils.Direction;
	import lyx.math.geometry.PanelGeometry;

	public class Tank extends DynamicGameElement
	{
		static public var TANK_VIEW_DATAS:Array;
		
		public var face_style:int = 0x0F9121;
		
		public var type:String = "Tank";
		
		public var _fire_interval:int = 250;
		private var _last_fire_time:int = 0;
		
		public function Tank(x:int=0,y:int=0,style:int=0x0F9121)
		{
			super(x,y,Direction.NORTH);	
			face_style = style;
			updateViewContext();
		}
		
		public function fire():Bullet
		{
			var bullet:Bullet = null;
			var fire_time:int = getTimer();
			if((fire_time-_last_fire_time)>_fire_interval)
			{
				_last_fire_time = fire_time;
				
				var point:Point = PanelGeometry.rotatePoint(Math.PI*(_direction+2)/4,new Point(20,0),new Point(20,20));
				
				bullet = new Bullet(point.x+x,point.y+y,_direction,type);
				bullet.active_area = new Rectangle(0,0,active_area.width,active_area.height);
			}
			return bullet;
		}
		
		override protected function outActiveAreaHandler():void
		{
			moveRollBack();
		}
		
		override protected function updateViewContext():void
		{
			if(TANK_VIEW_DATAS==null)
			{	
				TANK_VIEW_DATAS = [];
				var graphics_face:Sprite = new Sprite();
				var directions:Array = [Direction.EAST,Direction.SOUTH,Direction.WEST,Direction.NORTH];
				for(var i:int=0;i<directions.length;i++)
				{
					graphics_face.graphics.clear();
					graphics_face.graphics.beginFill(face_style);
					graphics_face.graphics.drawRect(0,0,40,40);
					
					var point:Point = PanelGeometry.rotatePoint(Math.PI*(directions[i]+2)/4,new Point(20,5),new Point(20,20));
					
					graphics_face.graphics.beginFill(0x888888);
					graphics_face.graphics.drawCircle(point.x,point.y,5);
					graphics_face.graphics.endFill();
					
					TANK_VIEW_DATAS[directions[i]] = new BitmapData(40,40,true,0);
					TANK_VIEW_DATAS[directions[i]].draw(graphics_face);
				}
			}
			
			view_data = TANK_VIEW_DATAS[_direction];
			
		}
	}
}